package com.hualan.sfzy.service.course.serviceimpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hualan.sfzy.constant.CommonConstant;
import com.hualan.sfzy.entity.course.Course;
import com.hualan.sfzy.entity.course.CourseVideo;
import com.hualan.sfzy.mapper.course.CourseMapper;
import com.hualan.sfzy.mapper.course.CourseVideoMapper;
import com.hualan.sfzy.service.course.CourseVideoService;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FrameGrabber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CourseVideoServiceImpl implements CourseVideoService {
    @Autowired
    private CourseVideoMapper courseVideoMapper;
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public int add(CourseVideo courseVideo) {
        // 获取视频时长
        try {
            String videoUrl = courseVideo.getVideoUrl();
            File videoFile = new File(videoUrl);
            if (videoFile.exists()) {

                FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoFile);
                grabber.start();
                int videoTime = (int) (grabber.getLengthInTime() / 1000000); // 转换为秒
                courseVideo.setVideoTime(videoTime);
                grabber.stop();

                // 更新课程总时长
                Course course = courseMapper.selectById(courseVideo.getCourseId());
                System.out.println(videoTime);
                if (course != null) {

                    course.setCourseTime(course.getCourseTime() + videoTime );
                    course.setVideoNumber(course.getVideoNumber() + 1);
                    System.out.println(course.getCourseTime());
                    courseMapper.update(course);
                }
            }
        } catch (FrameGrabber.Exception e) {
            e.printStackTrace();
        }
        return courseVideoMapper.insert(courseVideo);
    }

    @Override
    public int remove(Integer id) {
        CourseVideo courseVideo = courseVideoMapper.selectById(id);
        if (courseVideo != null) {

            System.out.println(courseVideo);
            // 更新课程总时长
            System.out.println("hhh"+id);
            CourseVideo courseVideo1 = courseVideoMapper.selectById1(id);

            System.out.println(courseVideo1);
            Course course = courseMapper.selectById(courseVideo1.getCourseId());
            System.out.println(course);
            if (course != null) {
                System.out.println("123");
                course.setCourseTime(course.getCourseTime() - courseVideo.getVideoTime()); // 转换为分钟
                course.setVideoNumber(course.getVideoNumber() -1 );
                System.out.println(course.getCourseTime());
                System.out.println(course.getVideoNumber());
                courseMapper.update(course);
            }
        }
        return courseVideoMapper.delete(id);
    }

    @Override
    public int removeBatch(Integer[] ids) {
        for (Integer id : ids) {
            CourseVideo courseVideo = courseVideoMapper.selectById(id);
            if (courseVideo != null) {
                // 更新课程总时长
                Course course = courseMapper.selectById(courseVideo.getCourseId());
                if (course != null) {
                    course.setCourseTime(course.getCourseTime() - courseVideo.getVideoTime() / 60); // 转换为分钟
                    courseMapper.update(course);
                }
            }
        }
        return courseVideoMapper.deleteBatch(ids);
    }

    @Override
    public int edit(CourseVideo courseVideo) {
        return courseVideoMapper.update(courseVideo);
    }

    @Override
    public CourseVideo findById(Integer id) {
        return courseVideoMapper.selectById(id);
    }

    @Override
    public List<CourseVideo> find() {
        return courseVideoMapper.select(null);
    }

    @Override
    public Map<String, Object> findByPage(Integer pageNum, Integer pageSize, CourseVideo courseVideo) {
        PageHelper.startPage(pageNum, pageSize);
        List<CourseVideo> list = courseVideoMapper.select(courseVideo);
        PageInfo<CourseVideo> pageInfo = new PageInfo<CourseVideo>(list);
        Map<String, Object> map = new HashMap<>();
        map.put(CommonConstant.PAGE_LIST, list);
        map.put(CommonConstant.PAGE_TOTAL, pageInfo.getPages());
        map.put(CommonConstant.TOTAL, pageInfo.getTotal());
        return map;
    }

    @Override
    public CourseVideo findById1(Integer id) {
        return courseVideoMapper.selectById1(id);
    }
}